<template>
  <div class="vinput-box" :class="itemdata.lCanOutput === '0' ? 'disable-color' : ''">
    <span style="color:red;margin-right: 5px;" v-if="itemdata.lMustInput === '1'">*</span>
    <template v-if="ishead">
      <template v-if="itemdata.lCanOutput === '0'">
        <input @focus="iptFoucus" v-model="itemdata.value" style="display: none;">
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input @focus="iptFoucus" type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" :id="itemdata.sField + '_bool'" required placeholder="系统自动填充" readonly v-model="itemdata.value"  :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input @focus="iptFoucus" type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" placeholder="请输入" :id="itemdata.sField + '_bool'" required v-model="itemdata.value"  @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input @focus="iptFoucus" type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" :id="itemdata.sField + '_bool'" placeholder="系统自动填充" readonly v-model="itemdata.value"  :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input @focus="iptFoucus" type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" placeholder="请输入" :id="itemdata.sField + '_bool'" v-model="itemdata.value"  @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else>
      <template v-if="itemdata.lCanOutput === '0'">
        <input @focus="iptFoucus" v-model="itemdata.value" style="display: none;">
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input @focus="iptFoucus" type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" :id="index + '_' + itemdata.sField + '_bool'" required placeholder="系统自动填充" readonly v-model="itemdata.value"  :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input @focus="iptFoucus" type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" placeholder="请输入" :id="index + '_' + itemdata.sField + '_bool'" required v-model="itemdata.value"  @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input @focus="iptFoucus" type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" :id="index + '_' + itemdata.sField + '_bool'" placeholder="系统自动填充" readonly v-model="itemdata.value"  :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input @focus="iptFoucus" type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <input @focus="iptFoucus"  class="vinput" size="mini" placeholder="请输入" :id="index + '_' + itemdata.sField + '_bool'" v-model="itemdata.value"  @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <van-action-sheet :get-container="getContainer"  v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-icon name="arrow" color="#C0C4CC"  />
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      enumshow: false,
      clickmodel: {},
      actions: []
    }
  },

  props: {
    jsondata: {
      type: Object,
      required: false
    },
    idobject: {
      type: String,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  // 监听没有生效
  watch:{
    'itemdata.valuehidden':{
      handler(newval){
        
          
        const value = newval==='是' ? '1' :'0'  
        this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: value, ishead: this.ishead, index: this.index })

      },
      immediate:true,
    }
  },
  methods: {
    iptFoucus () {
      document.activeElement.blur()
    },
    getContainer() {
      return document.querySelector('body')
    },
    selectedEnum (item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      // //console.log("^^^^",this.itemdata)
      // this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: item.id, ishead: this.ishead, index: this.index })
      // this.itemdata.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
      this.boolUpdate(this.clickmodel)
    },
    openBool (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      this.actions.push({ id: '1', name: '是' })
      this.actions.push({ id: '0', name: '否' })
      this.enumshow = true
    },
    boolUpdate (clickmodel) {
      
      let data = {}
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'sfdj') {
        let sfdjval = clickmodel.value === '是' ? '带价格 ' : ''
        let fcval = ''
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'fc') {
              fcval = itemgroup[itemindex].value
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'fc') {
        let sfdjval = ''
        let fcval = clickmodel.value
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'sfdj') {
              sfdjval = itemgroup[itemindex].value === '是' ? '带价格 ' : ''
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
    }
  }
}
</script>
